package com.yk.spark.service.manage.app.impl;

import com.yk.spark.dao.app.api.IAppDAO;
import com.yk.spark.dao.common.api.ICommonDAO;
import com.yk.spark.dao.file.api.IFileDAO;
import com.yk.spark.model.AppModel;
import com.yk.spark.model.FileModel;
import com.yk.spark.service.file.api.IFileService;
import com.yk.spark.service.manage.app.api.IAppService;
import com.yk.spark.util.SparkFileIO;
import com.yk.spark.util.UuidUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

/**
 * Created by Limit on 2017/8/23.
 */
@Service("appService")
public class AppServiceImpl implements IAppService{

    @Resource(name="commonDAO")
    private ICommonDAO commonDAO;

    @Resource(name="appDAO")
    private IAppDAO appDAO;

    @Resource(name = "fileService")
    private IFileService fileService;

    /**
     * 保存App
     *
     * @param appName    app名称
     * @param appType    app类型
     * @param parameters 配置参数
     * @return 操作状态，true：成功，false：失败
     */
    @Override
    public boolean saveApp(String appName, String appType, String parameters) {
        AppModel model=new AppModel();
        model.setAppName(appName);
        model.setAppType(appType);
        model.setAppParameters(parameters);
        model.setAppInput(new JSONArray().toString());
        model.setAppOutput("");
        model.setCreateTime(new Timestamp(new Date().getTime()));
        model.setUpdateTime(new Timestamp(new Date().getTime()));
        return this.commonDAO.save(model);
    }

    /**
     * 删除APP
     *
     * @param appID App唯一标识
     * @return 操作状态，true：成功，false：失败
     */
    @Override
    public boolean deleteApp(String appID) {
        AppModel model=new AppModel();
        model.setAppId(appID);
        return this.commonDAO.delete(this.appDAO.queryByAppId(appID));
    }

    /**
     * 更新APP
     *
     * @param appId      app唯一标识
     * @param input      app输入
     * @param output     app输出
     * @return 操作状态，true：成功，false：失败
     */
    @Override
    public boolean updateApp(String appId, String input, String output) {
        AppModel model=this.getAppModel(appId);
        FileModel fileModel=new FileModel();
        fileModel.setSize(0.0);
        fileModel.setCreateTime(new Timestamp(new Date().getTime()));
        fileModel.setUpdateTime(new Timestamp(new Date().getTime()));
        fileModel.setFileName("");
        fileModel.setFilePath(output);
        fileModel.setRemark("spark应用-"+model.getAppName()+"-数据目录");
        this.commonDAO.save(fileModel);
        model.setAppInput(input);
        model.setAppOutput(output);
        model.setUpdateTime(new Timestamp(new Date().getTime()));
        return this.commonDAO.update(model);
    }

    /**
     * 获取所有App
     *
     * @return app列表
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<AppModel> getAppModel() {
        return this.appDAO.queryAll();
    }

    /**
     * 获取分页App
     *
     * @param start 分页起始位置
     * @param limit 最大查询结果数
     * @return 本次分页APP的列表
     */
    @Override
    public List<AppModel> getAppModel(int start, int limit) {
        return this.appDAO.queryPage(start,limit);
    }

    /**
     * 根据AppID获取App
     *
     * @param appID AppModel唯一标识
     * @return AppModel实体
     */
    @Override
    public AppModel getAppModel(String appID) {
        return this.appDAO.queryByAppId(appID);
    }

    /**
     * 查询app总数
     *
     * @return app总数
     */
    @Override
    public long querySum() {
        return this.appDAO.querySum();
    }

    @Override
    public Map<String,Object> getSelectFile(JSONArray selected) throws JSONException {

        List<FileModel> models=this.fileService.getFileModel();

        HashSet<String> selectedSet=new HashSet<>();
        for (int i = 0; i <selected.length() ; i++) {
            selectedSet.add(selected.getString(i));
        }
        List<FileModel> selectedList=new ArrayList<>();
        List<FileModel> unselectedList=new ArrayList<>();
        for(FileModel model:models){
          if(selectedSet.contains(model.getFileId())){
              selectedList.add(model);
          }else {
              unselectedList.add(model);
          }
        }
        Map<String,Object> result=new HashMap<>();
        result.put("selected",selectedList);
        result.put("unselected",unselectedList);
        return result;
    }
}
